//
//  StoreLayout.m
//  BeautyClother
//
//  Created by dllo on 15/9/18.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "StoreLayout.h"
#import "ItemsModel.h"
@interface StoreLayout ()

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;
@property (nonatomic, assign) CGFloat RealWidth;
@end

@implementation StoreLayout

- (void)dealloc
{
    [_array release];
    [_dicArray release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
        self.RealWidth = 180 * _offWidth;
        self.array = [NSMutableArray array];
        self.dicArray = [NSMutableArray array];
    }
    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
    [self calculatorHeight];
}

- (CGSize)collectionViewContentSize
{
    CGFloat offwidth = [UIScreen mainScreen].bounds.size.width / 375;
    if (_offRight > _offLeft) {
        return CGSizeMake(315 * offwidth, _offRight);
        
    } else
    {
        return CGSizeMake(315 * offwidth, _offLeft);
        
    }
}

/// 计算图片cell的自适应高度
- (void)calculatorHeight
{
    [self.array removeAllObjects];
    _offLeft = 0;
    _offRight = 0;
    NSInteger num = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < num; i++) {
        NSIndexPath *iiii = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:iiii];
        
        
        if ([[self.dicArray objectAtIndex:i]isKindOfClass:[ItemsModel class]]) {
            
            ItemsModel *model = [self.dicArray objectAtIndex:i];
            
            CGFloat he = model.height;
            CGFloat height = he * _RealWidth / model.width + 45*_offHeight;
            
            if (i % 2 == 0) {
                att.frame = CGRectMake(5*_offWidth, 10*_offHeight + _offLeft, _RealWidth, height);
                _offLeft = _offLeft + height + 5*_offHeight;
            }else
            {
                att.frame = CGRectMake(190*_offWidth, 10*_offHeight + _offRight, _RealWidth, height);
                _offRight = _offRight + height + 5*_offHeight;
            }
            
            
        }else
        {
            NSDictionary *dic = [NSDictionary dictionary];
            
            dic = [self.dicArray objectAtIndex:i];
            
            CGFloat he = [[dic objectForKey:@"height"]floatValue];
            
            CGFloat height = he * _RealWidth / [[dic objectForKey:@"width"]floatValue] + 45*_offHeight;
            
            if (i % 2 == 0) {
                att.frame = CGRectMake(5*_offWidth, 10*_offHeight + _offLeft, _RealWidth, height);
                _offLeft = _offLeft + height + 5*_offHeight;
            }else
            {
                att.frame = CGRectMake(190*_offWidth, 10*_offHeight + _offRight, _RealWidth, height);
                _offRight = _offRight + height + 5*_offHeight;
            }
            
        }
        
        [self.array addObject:att];
    }
    
    
    
    
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *arr = [NSMutableArray array];
    for (UICollectionViewLayoutAttributes *att in self.array) {
        if (CGRectIntersectsRect(att.frame, rect) || CGRectContainsRect(att.frame, rect)) {
            [arr addObject:att];
            
        }
    }
    return arr;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return [self.array objectAtIndex:indexPath.item];
    
}

@end
